<template>
    <el-dialog
        :before-close="resetForm"
        :close-on-click-modal="false"
        :visible.sync="visible"
        append-to-body
        title="编辑文章发布信息"
        width="560px"
    >
        <div class="edit_page_div">
            <el-form ref="form" :model="form" :rules="rules" :show-message="false" label-width="100px">
                <el-form-item label="目录类型" prop="typeId">
                    <select-option v-model="form.typeId" select-width="400px" :list="typeList"></select-option>
                </el-form-item>
                <el-form-item label="文章编号" prop="articleCode">
                    <el-input v-model="form.articleCode" style="width: 400px;" placeholder="系统生成" disabled></el-input>
                </el-form-item>
                <el-form-item label="文章标题" prop="articleName">
                    <el-input v-model="form.articleName" style="width: 400px;"></el-input>
                </el-form-item>
                <el-form-item label="文章内容" prop="articleContent">
                    <el-input v-model="form.articleContent" style="width: 400px;" type="textarea" rows="3"></el-input>
                </el-form-item>
                <el-form-item label="文章封面" prop="imagePath">
                    <div class="el_image_upload" style="width: 400px;">
                        <el-button type="text" @click="uploadImg">上传图片</el-button>
                        <img :src="$store.state.fileURL + form.imagePath" height="30px" width="30px" alt=""/>
                    </div>
                </el-form-item>
                <el-form-item label="备注" prop="remarks">
                    <el-input v-model="form.remarks" style="width: 400px;" type="textarea" rows="3"></el-input>
                </el-form-item>
            </el-form>
            <div class="edit_button">
                <el-button v-loading.fullscreen.lock="loading" type="primary" @click="submit">保 存</el-button>
                <el-button type="primary" @click="resetForm">取 消</el-button>
            </div>
            <upload-file :visible.sync="showUpload" limit="1" :suffixes="suffixes" @change="setFileList"></upload-file>
        </div>
    </el-dialog>
</template>

<script>
import SelectOption from "@/components/select/selectOption.vue";
import UploadFile from "@/components/upload/uploadFile.vue";

export default {
    name: "newsArticleEdit",
    components: {UploadFile, SelectOption},
    props: {
        visible: Boolean,
        id: String,
    },
    data() {
        return {
            loading: false,
            showUpload: false,

            form: {
                typeId: '',
                articleCode: '',
                articleName: '',
                articleContent: '',
                imagePath: '',
                remarks: '',
            },
            rules: {
                typeId: [{required: true, message: '不能为空', trigger: 'change'}],
                articleName: [{required: true, message: '不能为空', trigger: 'change'}],
                articleContent: [{required: true, message: '不能为空', trigger: 'change'}],
                imagePath: [{required: true, message: '不能为空', trigger: 'change'}],
            },

            typeList: [],
            suffixes: '',
        }
    },
    watch: {
        visible(newVal) {
            if (newVal) {
                this.getTypeList();
            }
        }
    },
    methods: {
        getTypeList() {
            this.loading = true;
            this.$axios.post("/CHILD_SYSTEM/newsArticleType/getAllList", {
                leafFlag: 0,
            }).then(res => {
                this.typeList = this.$common.getSelectList(res.data, "typeName", "id");
                if (this.id) {
                    this.getData();
                }
            }).finally(() => {
                this.loading = false;
            })
        },
        getData() {
            this.loading = true;
            this.$axios.get("/CHILD_SYSTEM/newsArticle/getOneEntity/" + this.id).then(res => {
                this.form = res.data;
            }).finally(() => {
                this.loading = false;
            })
        },
        uploadImg(){
            this.suffixes = ".jpg,.png,.gif";
            this.showUpload = true;
        },
        setFileList(data) {
            this.form.imagePath = data[0].filePath;
        },
        submit() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.loading = true;
                    this.$axios.post("/CHILD_SYSTEM/newsArticle/" + (this.id ? "update" : "insert"), this.form).then(() => {
                        this.resetForm();
                        this.$message.success(res.message);
                    }).finally(() => {
                        this.loading = false;
                    })
                } else {
                    this.$message.error('表单填写有误');
                    return false;
                }
            });
        },
        resetForm() {
            this.$refs.form.resetFields();
            this.form = this.$options.data().form;
            this.$emit('update:visible', false);
            this.$emit('refreshList');
        },
    }
}
</script>

<style scoped>

</style>